package com.example.ccj.b.c.custom.selectuser;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ccj.R;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 选择人员
 * 2024年11月17日23:42:03
 */
public class SelectedUserAdapter extends RecyclerView.Adapter<SelectedUserAdapter.ViewHolder>{
    ISelectUserListener iSelectUserListener;
    List<UserMode> selectedUserList;
    /**
     * 字段类型数组
     *
     * @param selectedUserList 已选择的人员列表
     * @param iSelectUserListener 通过此回调返回选择的字段编号
     */
    public SelectedUserAdapter(List<UserMode> selectedUserList, @NonNull ISelectUserListener iSelectUserListener){
        this.iSelectUserListener = iSelectUserListener;
        this.selectedUserList = selectedUserList;
    }

    @NonNull
    @Override
    public SelectedUserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectedUserAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).
                inflate(R.layout.adapater_selected_user, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectedUserAdapter.ViewHolder holder, int position) {
        UserMode userMode = selectedUserList.get(position);
        holder.name.setText(userMode.name);
        if (!StringUtils.isEmpty(userMode.icon)) ImageUtils.loadImage(holder.icon.getContext(),userMode.icon,holder.icon);
        else holder.icon.setImageResource(R.mipmap.select_user_icon);

        final int po = position;
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserMode userMode1 = notifyRemove(po);
                if (iSelectUserListener != null) iSelectUserListener.delete(userMode1.id,userMode1);
            }
        });

    }

    @Override
    public int getItemCount() {
        return selectedUserList == null ? 0 : selectedUserList.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final ImageView icon ,delete;
        private final TextView name;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.adapater_selected_user_name);
            icon = itemView.findViewById(R.id.adapater_selected_user_icon);
            delete = itemView.findViewById(R.id.adapater_selected_user_delete);
        }
    }


    private UserMode notifyRemove(int position){
        UserMode remove = selectedUserList.remove(position);
        notifyItemRemoved(position);
        //重新加载从删除位置往下的所有item，如果删除的是中间，自删除的位置到最后开始是做重新添加操作
        //避免下表越界等错误
        notifyItemRangeRemoved(position,getItemCount());
        return remove;
    }
}
